const App = getApp();
const func = require('../../../utils/func');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 选中的物流信息
        usersDelivery: {
            'name': '',
            'code': '',
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
        // 设置数据类型
        _this.setData({
            service_id: parseInt(options.service_id) > 0 ? parseInt(options.service_id) : 0,
        });

        // 查询订单商品是否可申请售后
        _this.getOrderGoodsService();
    },

    // 下拉刷新，重载数据
    onPullDownRefresh() {
        let _this = this;
        // 查询订单商品是否可申请售后
        _this.getOrderGoodsService();
        wx.stopPullDownRefresh();
    },

    // 查询订单商品是否可申请售后
    getOrderGoodsService: function() {
        let _this = this;
        let postData = {
            action: 'getService',
            service_id: _this.data.service_id,
        };
        App._requestApi(_this, App.globalData.config.shopOrderServiceActionUrl, postData,
            function (res) {
                _this.setData({
                    isLoading: false,
                    address: res.data.address ? res.data.address : [],
                    express: res.data.express ? res.data.express : [],
                    orderGoods: res.data.orderGoods ? res.data.orderGoods : [],
                    service: res.data.service ? res.data.service : [],
                    serviceLog: res.data.serviceLog ? res.data.serviceLog : [],
                    serviceType: res.data.serviceType ? res.data.serviceType : [],
                });
            },
            function (res) {
                App.showError(res.msg, function() {
                    wx.navigateBack({delta: 1});
                });
            }
        );
    },

    // 设置选中的物流信息
    bindRegionChange(e) {
        // 获取数据
        let _this = this;
        let index = e.detail.value;
        let express = _this.data.express[index];
        let usersDelivery = _this.data.usersDelivery;
        // 处理数据
        usersDelivery['name'] = express['express_name'];
        // 重载数据
        _this.setData({usersDelivery: usersDelivery});
    },

    // 物流单号
    contentInput(e) {
        // 获取数据
        let _this = this;
        let usersDelivery = _this.data.usersDelivery;
        usersDelivery['code'] = e.detail.value;
        // 重载数据
        _this.setData({usersDelivery: usersDelivery});
    },

    // 提交物流信息
    submitExpress() {
        // 获取数据
        let _this = this;
        let usersDelivery = _this.data.usersDelivery;
        let postData = {
            action: 'addUsersDelivery',
            service_id: _this.data.service_id,
            order_id: _this.data.orderGoods.order_id,
            usersDelivery: JSON.stringify(usersDelivery),
        };
        App._requestPost(_this, App.globalData.config.shopOrderServiceActionUrl, postData,
            function (res) {
                if (parseInt(res.code) === 1) {
                    App.showSuccess(res.msg, function() {
                        wx.navigateBack();
                    });
                } else {
                    App.showError(res.msg);
                }
            },
            function (res) {
                App.showError(res.msg);
            }
        );
    },

})